package cn.db101.fairy.crawl.pipeline

import cn.db101.fairy.entity.FairyDetail
import com.mybatisflex.kotlin.extensions.db.insert
import com.mybatisflex.kotlin.extensions.db.queryOne
import com.mybatisflex.kotlin.extensions.kproperty.eq
import com.mybatisflex.kotlin.extensions.wrapper.whereWith
import org.springframework.stereotype.Component
import us.codecraft.webmagic.ResultItems
import us.codecraft.webmagic.Task
import us.codecraft.webmagic.pipeline.Pipeline
import java.time.LocalDateTime

@Component
class FairyPipeline: Pipeline {

    override fun process(resultItems: ResultItems?, task: Task?) {


        val name: String? = resultItems?.get("name")



        // 先查询对应的名称是否存在，不存在则插入
        if(queryOne<FairyDetail> {
                whereWith {
                    FairyDetail::name eq name
                }
            } != null) {
            return
        }

        insert(FairyDetail().also {
            it.name = name
            it.content = resultItems?.get("content")
            it.categoryId = resultItems?.get("categoryId")
            it.createTime = LocalDateTime.now()
        })

    }
}